package com.fly.web;

import com.fly.base.Result;
import com.fly.base.ResultGenerator;
import com.fly.domain.model.CustomerFeedbackInfo;
import com.fly.domain.service.CustomerFeedbackInfoService;
import com.fly.domain.service.CustomerService;
import com.fly.util.SidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.util.Optional;

import static com.fly.util.SidService.Biz.CUSTOMER;

/**
 * Created by frinder on 2018/4/3.
 */
@RestController
@RequestMapping("/myCustomerFeedbackInfo")
public class MyCustomerFeedbackInfoController {

    @Autowired
    private CustomerService customerService;

    @Resource
    private CustomerFeedbackInfoService customerFeedbackInfoService;

    @Autowired
    private SidService sidService;

    @PostMapping("/add")
    public Result add(CustomerFeedbackInfo customerFeedbackInfo) {
        Optional.ofNullable(customerService.findBy("sid", customerFeedbackInfo.getCustomerSid()))
                .ifPresent(s -> {
                    customerFeedbackInfo.setCustomerName(s.getName());
                    customerFeedbackInfo.setSid(sidService.getSid(CUSTOMER));
                    customerFeedbackInfoService.save(customerFeedbackInfo);
                });
        return ResultGenerator.genSuccessResult();
    }

}
